package system

import (
	"errors"
	"fmt"
	"jgyApi/server/global"
	"jgyApi/server/model/system"
	"jgyApi/server/utils"
)

type UserService struct{}



//@author: [piexlmax](https://github.com/piexlmax)
//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: Login
//@description: 用户登录
//@param: u *model.SysUser
//@return: err error, userInter *model.SysUser

func (userService *UserService) Login(u *system.AuthAdmin) (userInter *system.AuthAdmin, err error) {
	if nil == global.GVA_DB {
		return nil, fmt.Errorf("db not init")
	}

	var user system.AuthAdmin
	//err = global.GVA_DB.Where("username = ?", u.Username).Preload("Authorities").Preload("Authority").First(&user).Error
	//err = global.GVA_DB.Where("username = ?", u.Username).First(&user).Error
	err = global.GVA_DB.Where("username = ?", u.Username).Preload("AuthGroup").Preload("Vip").First(&user).Error


	if err == nil {
		if ok := utils.Md5Check(u.Password, user.Password); !ok {
			return nil, errors.New("密码错误")
		}

		//MenuServiceApp.UserAuthorityDefaultRouter(&user)
	}
	return &user, err
}

